package com.dks.repository.impl.pay;

import java.time.LocalDateTime;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aliyuncs.utils.StringUtils;
import com.dks.enumeration.CashCouponStatus;
import com.dks.mapper.CashCouponMapper;
import com.dks.models.pay.CashCoupon;
import com.dks.repository.ICashCouponRepository;
import com.dks.result.*;
import com.dks.util.PageInfo;
import com.dks.vo.pay.CashCouponVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Repository
public class CashCouponRepository implements ICashCouponRepository {

    @Autowired
    private CashCouponMapper cashCouponMapper;


    @Override
    public ResultModel<PageInfo<CashCouponVo>> myList(long userId, Long classHourPackageId,
                                                      CashCouponStatus cashCouponStatus, int pageNum, int pageSize){
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<String> statusList = new ArrayList<>();
        if (cashCouponStatus != null){
            switch (cashCouponStatus){
                case AVAILABLE: statusList.add(CashCouponStatus.AVAILABLE.name()); break;
                case USED: statusList.add(CashCouponStatus.USED.name()); break;
                case EXPIRE: statusList.add(CashCouponStatus.EXPIRE.name()); break;
                case NOT_REDUCTION: statusList.add(CashCouponStatus.NOT_REDUCTION.name()); break;
                case NOT_AMOUNT: statusList.add(CashCouponStatus.NOT_AMOUNT.name()); break;
                case NOT_CLASS_HOUR_PACKAGE: statusList.add(CashCouponStatus.NOT_CLASS_HOUR_PACKAGE.name()); break;
                case NOT_AVAILABLE: {
                    statusList.add(CashCouponStatus.USED.name());
                    statusList.add(CashCouponStatus.EXPIRE.name());
                    statusList.add(CashCouponStatus.NOT_REDUCTION.name());
                    statusList.add(CashCouponStatus.NOT_AMOUNT.name());
                    statusList.add(CashCouponStatus.NOT_CLASS_HOUR_PACKAGE.name());
                    break;
                }
                default: break;
            }
        }
        List<CashCouponVo> myCashCouponList;
        if (io.jsonwebtoken.lang.Collections.isEmpty(statusList)){
            myCashCouponList = cashCouponMapper.myList(userId, classHourPackageId, null);
        }else{
            myCashCouponList = cashCouponMapper.myList(userId, classHourPackageId, statusList);
        }
        //过滤筛选+拼凑结果数据
//        List<CashCouponVo> cashCouponVoList = myCashCouponList.stream().filter(cashCouponDto -> {
//            if (cashCouponStatus == null){
//                return true;
//            }
//            //按筛选条件过滤
//            switch (cashCouponStatus){
//                case AVAILABLE: return CashCouponStatus.AVAILABLE.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                case USED: return CashCouponStatus.USED.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                case EXPIRE: return CashCouponStatus.EXPIRE.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                case NOT_REDUCTION: return CashCouponStatus.NOT_REDUCTION.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                case NOT_AMOUNT: return CashCouponStatus.NOT_AMOUNT.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                case NOT_CLASS_HOUR_PACKAGE: return CashCouponStatus.NOT_CLASS_HOUR_PACKAGE.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                case NOT_AVAILABLE: return CashCouponStatus.USED.name().equals(cashCouponDto.getCashCouponStatus())
//                                            || CashCouponStatus.EXPIRE.name().equals(cashCouponDto.getCashCouponStatus())
//                                            || CashCouponStatus.NOT_REDUCTION.name().equals(cashCouponDto.getCashCouponStatus())
//                                            || CashCouponStatus.NOT_AMOUNT.name().equals(cashCouponDto.getCashCouponStatus())
//                                            || CashCouponStatus.NOT_CLASS_HOUR_PACKAGE.name().equals(cashCouponDto.getCashCouponStatus()) ? true : false;
//                default: return true;
//            }
//        }).collect(Collectors.toList());

        PageInfo<CashCouponVo> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), myCashCouponList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    //判断代金券是否已经使用
    public boolean isUsedStatus(int isUsedStatus){
        if (isUsedStatus != 0){
            return true;
        }
        return false;
    }

    //判断代金券是否过期  buyTime 购买时间
    public boolean isOverTime(LocalDateTime expireTime){
        if (expireTime.isBefore(LocalDateTime.now())){
            return true;
        }
        return false;
    }

    //判断代金券是否满足满减
    public boolean isReductionEnough(CashCoupon cashCoupon, int balance){
        if (cashCoupon.getFullReductionStatus() == 0 && cashCoupon.getFullReductionMoney() > balance){
            return false;
        }
        return true;
    }

    //判断是否大于优惠金额
    public boolean isAmountEnough(CashCoupon cashCoupon, int balance){
        return cashCoupon.getPreferentialAmount() < balance;
    }

    //判断是否当前课包可用
    public boolean isClassHourPackage(CashCoupon cashCoupon, long classHourPackageId){
        if (!StringUtils.isEmpty(cashCoupon.getClassHourPackageId())){
            return true;
        }
        return Arrays.asList(cashCoupon.getClassHourPackageId().split(",")).contains(classHourPackageId);
    }

}



